package com.xyht.sca_s.student_manage_system.modules.stampRegistration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.stampRegistration.entity.SmsStampRegistrationProcess;
import com.xyht.sca_s.student_manage_system.modules.stampRegistration.entity.dto.SmsStampRegistrationProcessDto;
import com.xyht.sca_s.student_manage_system.modules.stampRegistration.entity.vo.SmsStampRegistrationProcessVo;
import com.xyht.sca_s.student_manage_system.modules.stampRegistration.entity.vo.SmsStampRegistrationUserVo;
import com.xyht.sca_s.student_manage_system.modules.stampRegistration.mapper.SmsStampRegistrationProcessMapper;
import com.xyht.sca_s.student_manage_system.modules.stampRegistration.service.SmsStampRegistrationProcessService;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsRole;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.stampRegistration.constant.StampObjectTypeConstant.STAMP_OBJECT_TYPE_PERSON;
import static com.xyht.sca_s.student_manage_system.modules.stampRegistration.constant.StampObjectTypeConstant.STAMP_OBJECT_TYPE_ROLE;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-05-25
 */
@Service
public class SmsStampRegistrationProcessServiceImpl extends ServiceImpl<SmsStampRegistrationProcessMapper, SmsStampRegistrationProcess> implements SmsStampRegistrationProcessService {
    @Autowired
    private SmsStampRegistrationProcessMapper smsStampRegistrationProcessMapper;
    @Autowired
    private CacheUtil cacheUtil;
    @Autowired
    private SmsRoleMapper smsRoleMapper;

    @Override
    public ResponseResult getStampRegistrationProcessList() {
        List<SmsStampRegistrationProcessVo> procurementProcessVoList = smsStampRegistrationProcessMapper.selectList(
                new QueryWrapper<SmsStampRegistrationProcess>()
                .lambda()
                .orderByAsc(SmsStampRegistrationProcess::getProcessSort))
                .stream()
                .map(process ->{
                    SmsStampRegistrationProcessVo smsStampRegistrationProcessVo = new SmsStampRegistrationProcessVo();
                    BeanUtils.copyProperties(process,smsStampRegistrationProcessVo);
                    if(process.getProcessObjectType() !=null && !isNullOrEmpty(process.getProcessObjectId())){
                        if(process.getProcessObjectType() == STAMP_OBJECT_TYPE_PERSON){
                            SmsUser userInfo = cacheUtil.getUserInfo(process.getProcessObjectId());
                            if(userInfo != null && !isNullOrEmpty(userInfo.getId())){
                                SmsStampRegistrationUserVo userVo = new SmsStampRegistrationUserVo();
                                BeanUtils.copyProperties(userInfo,userVo);
                                smsStampRegistrationProcessVo.setUserInfo(userVo);
                                smsStampRegistrationProcessVo.setProcessObjectName(userInfo.getRealName());
                            }
                        }else {
                            SmsRole smsRole = smsRoleMapper.selectById(process.getProcessObjectId());
                            if(smsRole != null && !isNullOrEmpty(smsRole.getId())){
                                smsStampRegistrationProcessVo.setProcessObjectName(smsRole.getRoleName());
                            }
                        }
                    }
                    return smsStampRegistrationProcessVo;
                        })
                .collect(Collectors.toList());
        return CommonResult.success(procurementProcessVoList);
    }

    @Override
    public ResponseResult updateStampRegistrationProcess(SmsStampRegistrationProcessDto processDto) {
        if(isNullOrEmpty(processDto.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        if(isNullOrEmpty(processDto.getProcessObjectType())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //如果在可编辑情况下
        //审批对象类型不为空时,审批对象id可为空
        if (processDto.getProcessIsApplicantEdit()) {
            if (processDto.getProcessObjectType() == STAMP_OBJECT_TYPE_ROLE) {
                if (isNullOrEmpty(processDto.getProcessObjectId())) {
                    return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
                }
            }
        } else {
            if (isNullOrEmpty(processDto.getProcessObjectId())) {
                return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
            }
        }

        SmsStampRegistrationProcess smsStampRegistrationProcess = smsStampRegistrationProcessMapper.selectById(processDto.getId());
        if(smsStampRegistrationProcess == null || isNullOrEmpty(smsStampRegistrationProcess.getId())){
            return CommonResult.failed(CommonCodeEnum.STAMP_REGISTRATION_NOT_EXIST);
        }
        //判断角色或个人是否存在
        if(processDto.getProcessObjectType() != null && !isNullOrEmpty(processDto.getProcessObjectId())){
            if(processDto.getProcessObjectType() == STAMP_OBJECT_TYPE_PERSON){
                SmsUser userInfo = cacheUtil.getUserInfo(processDto.getProcessObjectId());
                if(userInfo == null || isNullOrEmpty(userInfo.getId())){
                    return CommonResult.failed(CommonCodeEnum.STAMP_REGISTRATION_PROCESS_USER_NOT_EXIST);
                }else if(processDto.getProcessObjectType() == STAMP_OBJECT_TYPE_ROLE){
                    SmsRole smsRole = smsRoleMapper.selectById(processDto.getProcessObjectId());
                    if(smsRole == null || isNullOrEmpty(smsRole.getId())){
                        return CommonResult.failed(CommonCodeEnum.STAMP_REGISTRATION_PROCESS_ROLE_NOT_EXIST);
                    }
                }
            }
        }

        SmsStampRegistrationProcess updateProcess = new SmsStampRegistrationProcess();
        updateProcess.setId(processDto.getId());
        if (!isNullOrEmpty(processDto.getProcessObjectType())) {
            updateProcess.setProcessObjectId(processDto.getProcessObjectId());
            updateProcess.setProcessObjectType(processDto.getProcessObjectType());
        }
        if (!isNullOrEmpty(processDto.getProcessIsApplicantEdit())) {
            updateProcess.setProcessIsApplicantEdit(processDto.getProcessIsApplicantEdit());
        }
        if (!isNullOrEmpty(processDto.getProcessIsSign())) {
            updateProcess.setProcessIsSign(processDto.getProcessIsSign());
        }
        if (!isNullOrEmpty(processDto.getProcessIsStamp())) {
            updateProcess.setProcessIsStamp(processDto.getProcessIsStamp());
        }
        if (!isNullOrEmpty(processDto.getProcessIsUse())) {
            updateProcess.setProcessIsUse(processDto.getProcessIsUse());
        }

        smsStampRegistrationProcessMapper.updateById(updateProcess);
        return CommonResult.success();
    }
}
